//
//  KYHomeViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/10.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYHomeViewController.h"
#import "UIImage+EX.h"
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"
#import "Header.h"
#import <SDCycleScrollView.h>
#import "KYCycleModel.h"
#import "KYCycleModalViewController.h"

#import "KYWeatherView.h"
#import "KYMiddleView.h"
#import "KYCollectionView.h"
#import "KYDiseaseDetailModel.h"


#import "KYProvinceViewController.h"
#import "KYSelectBasicViewController.h"
#import "KYClassModel.h"
#import "KYPublicServeViewController.h"

@interface KYHomeViewController ()<KYWeatherViewDelegate,SDCycleScrollViewDelegate,UICollectionViewDelegate>
@property (nonatomic, strong)KYWeatherView *weatherVeiw;
@property (nonatomic, strong)KYMiddleView *middleView;
@property (nonatomic, strong)SDCycleScrollView *cycleView;
@property (nonatomic, strong)KYCollectionView *collection;
@property (nonatomic, strong)NSArray *cycleModel;
//@property (nonatomic, copy)NSString *place;
@property (nonatomic, strong)NSArray *classModel;
@end

@implementation KYHomeViewController
#pragma -mark 懒加载
-(NSArray *)classModel{
    if (!_classModel) {
        _classModel = [KYClassModel getClassModel];
    }
    return _classModel;
}
-(KYWeatherView *)weatherVeiw{
    if (!_weatherVeiw) {
        _weatherVeiw = [[KYWeatherView alloc]init];
        _weatherVeiw.delegate = self;
    }
    return _weatherVeiw;
}
-(KYMiddleView *)middleView{
    if (!_middleView) {
        _middleView = [[KYMiddleView alloc]init];
    }
    return _middleView;
}
-(KYCollectionView *)collection{
    if (!_collection) {
        _collection = [[KYCollectionView alloc]init];
    }
    return _collection;
}

//第一次也会调用push方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    [self setUpNavBar];
    [self setUpWeatherView];
    [self setUpCycleScrollView];
    [self setUpMiddle];
    [self setUpCollection];
    NSString *place = [[NSUserDefaults standardUserDefaults] valueForKey:@"place"];
    if (place == nil) {
        place = @"北京";
    }
    [self.weatherVeiw.placeName setTitle:place forState:UIControlStateNormal];
    [self.weatherVeiw getWeatherDataWithPlaceName:place];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(placeBtnNotification:) name:@"placeNotification" object:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - 切换城市通知方法
-(void)placeBtnNotification:(NSNotification *)notification{
    NSString *place = notification.userInfo[@"place"];
    [self.weatherVeiw.placeName setTitle:place forState:UIControlStateNormal];
    [self.weatherVeiw getWeatherDataWithPlaceName:place];
    [[NSUserDefaults standardUserDefaults]setValue:place forKey:@"place"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

#pragma mark - 设置主页导航栏(重写父类方法)
-(void)addBackButton{
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"l"] orginalImage] style:UIBarButtonItemStylePlain target:self action:@selector(seliderBtnKlick)];
    self.navigationItem.leftBarButtonItem = left;
    self.title = @"快医";
}

-(void)seliderBtnKlick{
//    NSLog(@"抽屉效果");
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

#pragma mark - 设置天气
-(void)setUpWeatherView{
    self.weatherVeiw.frame = CGRectMake(0, 0, ScreenWith, 70.5);
    [self.view addSubview:self.weatherVeiw];
}
#pragma mark - 设置轮播器
-(void)setUpCycleScrollView{
    CGFloat cycleY = CGRectGetMaxY(self.weatherVeiw.frame);
    CGFloat cycleH = ScreenHeigth/2 - cycleY - 30;
    NSString *url = @"http://iosapi.itcast.cn/banners.json.php";
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f",@"page_size":@(10),@"page":@(1)};
     NSMutableArray *mutArray = [NSMutableArray array];
    [KYCycleModel cycleWithUrl:url parma:paramDic success:^(NSArray *cycleModel) {
        self.cycleModel = cycleModel;
        for (KYCycleModel *model in cycleModel) {
            [mutArray addObject:model.banner_img_url];
        }
//        self.imgUrls = mutArray.copy;
//        NSLog(@"%@",self.imgUrls);
    } failure:^(NSError *error) {
        NSLog(@"%@",error.description);
    }];
    CGRect frame = CGRectMake(0, cycleY, ScreenWith, cycleH);
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:frame imageURLStringsGroup:nil];
     cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//    cycleScrollView.titlesGroup = @[@"11111",@"22222",@"33333"];
    cycleScrollView.delegate = self;
    cycleScrollView.autoScrollTimeInterval = 2;
    //需要延时才能显示图片
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = mutArray;
    });
    self.cycleView = cycleScrollView;
    [self.view addSubview:cycleScrollView];
    
}
//代理方法
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    KYCycleModalViewController *cycleModalVC = [[KYCycleModalViewController alloc]init];
    [self.navigationController pushViewController:cycleModalVC animated:YES];
    KYCycleModel *cycleModel = self.cycleModel[index];
    cycleModalVC.link = cycleModel.banner_link;
    cycleModalVC.title = cycleModel.banner_title;
    
}
#pragma mark - 设置中间View
-(void)setUpMiddle{
    CGFloat middleY = CGRectGetMaxY(self.cycleView.frame);
    self.middleView.frame = CGRectMake(0, middleY, ScreenWith, 44.5);
    [self.view addSubview:self.middleView];
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor grayColor];
    line.frame = CGRectMake(0, middleY, ScreenWith, 1);
    [self.view addSubview:line];

}
#pragma mark - 设置底部Collection
-(void)setUpCollection{
    CGFloat collectionY = CGRectGetMaxY(self.middleView.frame);
    CGFloat collectionH = ScreenHeigth - CGRectGetMaxY(self.middleView.frame);
    self.collection.frame = CGRectMake(0, collectionY, ScreenWith, collectionH);
//    self.collection.backgroundColor = [UIColor redColor];
    self.collection.delegate = self;
    [self.view addSubview:self.collection];
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor grayColor];
    line.frame = CGRectMake(0, collectionY, ScreenWith, 1);
    [self.view addSubview:line];
}
#pragma mark - collectionView代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 5) {
        KYPublicServeViewController *publicVC = [[KYPublicServeViewController alloc]init];
        publicVC.title = @"公益活动";
        [self.navigationController pushViewController:publicVC animated:YES];
    }else{
        KYSelectBasicViewController *selectVC = [[KYSelectBasicViewController alloc]init];
        selectVC.title = @"疾病详情选择";
        selectVC.ci1 = indexPath.item+1;
//        NSLog(@"%zd",selectVC.ci1);
        selectVC.diseareText = [self.classModel[indexPath.row] name];
        [self.navigationController pushViewController:selectVC animated:YES];
    }
}

#pragma mark - 城市按钮点击代理方法
-(void)placeBtnKlicked:(KYWeatherView *)weatherView{
    [self.navigationController pushViewController:[KYProvinceViewController new] animated:YES];
}
@end
